<template>
  <div class="layout-col">
    <div class="layout-col bss">
      <div class="ww100 flex-bc plr12 ptb8 bs4">
        <div class="flex-sc">
          <span class="f15 fw">电压等级</span>
        </div>
        <div class="flex-ec flex1 h30">
          <div class="rad4 ptb6 plr10 flex-cc cursor bgi3 white ml10" 
            v-if="state.content.find(v=>{return typeof(v.key) == 'object'}) && state.content.find(v=>{return typeof(v.key) == 'object'})['key'].hasOwnProperty('add')" 
            @click.stop="handleClick('add', state.addItem)">
            <i-ep-plus class="f12 fw" /><span class="f14 ml5">{{state.content.find(v=>{return typeof(v.key) == 'object'})['key'].add}}</span>
          </div>
        </div>
      </div>
      <div class="row bgi22">
        <div :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i" v-show="v.show" :class="typeof(v.key) == 'object'?v.width +' '+ 'mlr5':v.width">{{v.name}}</div>
      </div>
      <div class="table">
        <div class="row" :class="publicStore.active[state.key] == item[state.key]?'active':index%2 == 1?'bgi23':'bgi18'" v-for="(item, index) in state.list" :key="index" @click="handleClick('active', item)">
          <div v-show="v.show" :class="v.width" :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i">
            <span v-if="typeof(v.key) == 'object'" class="i21 cursor mlr5" :class="kk=='add'?'none':''" v-for="(vv, kk, ii) in v.key" :key="ii" @click.stop="handleClick(kk, item)">{{vv}}</span>
            <span v-else-if="v.key=='*'">{{index+1}}</span>
            <span v-else-if="v.type == 'time'">{{item[v.key] == 0?'/':parseTime(item[v.key])}}</span>
            <span v-else-if="v.type == 'find'" :class="find(v.value[0], [v.value[1], item[v.key]], 'color')?find(v.value[0], [v.value[1], item[v.key]], 'color'):''">
              {{find(v.value[0], [v.value[1], item[v.key]], v.value[2])}}
            </span>
            <span v-else-if="v.type == 'switch'">
              <el-switch v-model="item[v.key]" :active-text="v.value[1].name" :inactive-text="v.value[0].name" :active-value="v.value[1].value" :inactive-value="v.value[0].value" inline-prompt @change="handleClick('switch', item)"></el-switch>
            </span>
            <span v-else-if="v.type == 'input'"><el-input v-model="item[v.value]" placeholder="请输入" style="width: 150px;" size="small" /></span>
            <span v-else>{{isNull(item[v.key]) && v.key!=''? '/' : item[v.key]}}</span>
          </div>
        </div>
        <div v-if="state.empty" class="flex-cc ww100 tc ptb13 bgi18">没有数据</div>
      </div>
      <Add @init="init" :state="state" ref="exposeRef" />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import Add from '@/components/Add'
	const { proxy }:any = getCurrentInstance()
  const publicStore = proxy.publicStore()
  const configStore = proxy.configStore()
  const state = reactive({
	  ...publicStore.$state,
    voltageList: [],
    key: 'bayNum',
    query: {"o": "o_wf_bay", "p": [publicStore.distributId]},
    content: [
      { width: 'w70',   show: true, align: 'center', key: '*', name: '序号' },
      { width: 'w50x5', show: true, align: 'left',  key: 'bayNum', name: '编号' },
      { width: 'w50x5', show: true, align: 'left',  key: 'bayName', name: '名称' },
      { width: 'w50x5', show: true, align: 'left',  key: 'voltage', name: '电压等级(V)', type: 'find', value: [[], 'voltage', 'name'] },
      { width: 'w50x5', show: true, align: 'left',  key: 'sortPriority', name: '排序' },
      { width: 'flex1', show: true, align: 'right', key: {add: '新增', edit:'编辑', del: '删除'},  name: '操作' },
    ],
    editField: {},
    editFrom: [
      { required: true, editshow: true, name: '编号', key: 'bayNum', type: ['input'] },
      { required: true, editshow: true, name: '名称', key: 'bayName', type: ['input'] },
      { required: true, editshow: true, name: '电压', key: 'voltage',  type: ['select', [], 'voltage', 'name'] },
      { required: true, editshow: true, name: '排序', key: 'sortPriority', type: ['input'] },
    ],
    addItem: {},
  })
  let exposeRef = $ref()

  onMounted(async() => {
    let params = {queryApi2: {"o": "o_wf_voltage"}}
    state.voltageList = await publicStore.post(params)
    init()
  })

  const setInit = () => {
    state.content.forEach(v => {
      if(v.key == 'voltage') v.value[0] = state.voltageList
    })
    state.editFrom.forEach(v => {
      if(v.key == 'voltage') v.type[1] = state.voltageList
    })
    state.addItem = {stationNum: publicStore.distributId}
  }

  const init = async(key) => {
    state.params = {queryApi: state.query}
    let res = await publicStore.post(state.params)
    state.empty = proxy.isNull(res)? true : false
    state.list = proxy.isNull(res)? [] : res
    publicStore._public.list = state.list
    setInit()
  }

  const handleClick = (remark, val) => {
    if(remark == 'add' || remark == 'edit'){
      exposeRef.onVisable(val)
    }
    if(remark == 'del'){
      exposeRef.del(val, state.query, state.key)
    }
  }
</script>
  
<style scoped lang="scss">

</style>
  